package com.shop.user.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shop.entity.pojo.Brands;
import com.shop.user.mapper.BrandsMapper;
import com.shop.user.service.BrandsService;
import com.shop.utils.common.enums.Message;
import com.shop.utils.common.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class BrandsServiceImpl extends ServiceImpl<BrandsMapper, Brands> implements BrandsService {

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private BrandsMapper brandsMapper;

    @Override
    public Result<List<Brands>> getBrandsList() {
        long start = System.currentTimeMillis();
        log.info("微信小程序-获取分类列表");
        String key= Message.WeChat_Key_Brands_All;
        List<Brands> check = (List<Brands>) redisTemplate.opsForValue().get(key);
        if (check!=null&&check.size()>0) {
            log.info("浏览缓存");
            long end = System.currentTimeMillis();
            log.info("用户从缓存获取分类列表进程完成:{}",(end-start));
            return Result.okResult(check);
        }
        List<Brands> list = list();
        redisTemplate.opsForValue().set(key,list);
        log.info("浏览数据库");
        long end = System.currentTimeMillis();
        log.info("用户从数据库获取分类列表进程完成:{}",(end-start));
        return Result.okResult(list);
    }
}
